package com.ruoyi.basicData.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basicData.mapper.SupplierMasterDataMapper;
import com.ruoyi.basicData.domain.SupplierMasterData;
import com.ruoyi.basicData.service.ISupplierMasterDataService;

/**
 * 供应商管理Service业务层处理
 * 
 * @author admin
 * @date 2024-06-27
 */
@Service
public class SupplierMasterDataServiceImpl implements ISupplierMasterDataService 
{
    @Autowired
    private SupplierMasterDataMapper supplierMasterDataMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询供应商管理
     * 
     * @param supplierId 供应商管理主键
     * @return 供应商管理
     */
    @Override
    public SupplierMasterData selectSupplierMasterDataBySupplierId(Long supplierId)
    {
        return supplierMasterDataMapper.selectSupplierMasterDataBySupplierId(supplierId);
    }

    /**
     * 查询供应商管理列表
     * 
     * @param supplierMasterData 供应商管理
     * @return 供应商管理
     */
    @Override
    public List<SupplierMasterData> selectSupplierMasterDataList(SupplierMasterData supplierMasterData)
    {
        return supplierMasterDataMapper.selectSupplierMasterDataList(supplierMasterData);
    }

    /**
     * 新增供应商管理
     * 
     * @param supplierMasterData 供应商管理
     * @return 结果
     */
    @Override
    public int insertSupplierMasterData(SupplierMasterData supplierMasterData)
    {
        supplierMasterData.setCreateTime(DateUtils.getNowDate());
        return supplierMasterDataMapper.insertSupplierMasterData(supplierMasterData);
    }


    /**
     * 批量新增供应商管理
     *
     * @param supplierMasterDatas 供应商管理List
     * @return 结果
     */
    @Override
    public int batchInsertSupplierMasterData(List<SupplierMasterData> supplierMasterDatas)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(supplierMasterDatas)) {
            try {
                for (int i = 0; i < supplierMasterDatas.size(); i++) {
                    int row = supplierMasterDataMapper.insertSupplierMasterData(supplierMasterDatas.get(i));
                    // 防止内存溢出，没100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%3 == 0) || i == supplierMasterDatas.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改供应商管理
     * 
     * @param supplierMasterData 供应商管理
     * @return 结果
     */
    @Override
    public int updateSupplierMasterData(SupplierMasterData supplierMasterData)
    {
        return supplierMasterDataMapper.updateSupplierMasterData(supplierMasterData);
    }

    /**
     * 批量删除供应商管理
     * 
     * @param supplierIds 需要删除的供应商管理主键
     * @return 结果
     */
    @Override
    public int deleteSupplierMasterDataBySupplierIds(Long[] supplierIds)
    {
        return supplierMasterDataMapper.deleteSupplierMasterDataBySupplierIds(supplierIds);
    }

    /**
     * 删除供应商管理信息
     * 
     * @param supplierId 供应商管理主键
     * @return 结果
     */
    @Override
    public int deleteSupplierMasterDataBySupplierId(Long supplierId)
    {
        return supplierMasterDataMapper.deleteSupplierMasterDataBySupplierId(supplierId);
    }
}
